<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="email=no" />
    <title>首页</title>
    <link rel="stylesheet" href="../../css/api.css">
    <link rel="stylesheet" href="../../css/main.css">
    <style>
        .swiper-container {
            width: 100%;
            height: 100%;
        }

        .swiper-slide {
            /* Center slide text vertically */
            display: -webkit-box;
            display: -ms-flexbox;
            display: -webkit-flex;
            display: flex;
            -webkit-box-pack: center;
            -ms-flex-pack: center;
            -webkit-justify-content: center;
            justify-content: center;
            -webkit-box-align: center;
            -ms-flex-align: center;
            -webkit-align-items: center;
            align-items: center;
            transition: 300ms;
            transform: scale(0.8);
        }

        .swiper-slide img {
            box-shadow: 0 .04rem .1rem 0 rgba(0, 0, 0, 0.20);
            border-radius: .05rem;
        }

        .swiper-slide-active,
        .swiper-slide-duplicate-active {
            transform: scale(1);
        }

        .info img,
        .kf img {
            width: .22rem;
            height: .22rem;
            object-fit: fill;
        }

        .home-head .head .info {
            position: relative;
        }

        .home-head .head .info.redDot::after {
            content: ' ';
            background: #F53B31;
            position: absolute;
            top: -.035rem;
            right: -.035rem;
            border-radius: 50%;
            width: .07rem;
            height: .07rem;
        }
    </style>
</head>

<body>
    <div class="app" v-cloak>
       
    </div>
    <script src="../../script/api.js"></script>
    <script src="../../script/flexible.js"></script>
    <script src="../../script/fastclick.js"></script>
    <script src="../../script/vue.min.js"></script>
    <script src="../../script/iconfont.js"></script>
    <script src="../../script/restful.js"></script>
    <script src="../../script/businessCommon.js"></script>
    <script src="../../script/common.js"></script>
    <script>
        apiready = function () {
            var aMap = api.require('aMap');
            vm = new Vue({
                el: '.app',
                data: {
                    selectedCityName: "定位...",
                    kfUrl: '',
                    unReadNum: 0
                },
                created() {
                    api.sendEvent({
                        name: 'eventTab',
                        extra: { key: ' value' }
                    });
                },
                mounted() {
                    //  this.getUnReadNum()
                    this.openFrame()

                   // this.checkLocation()
                },

                methods: {
                    openFrame() {
                        let vm = this
                        var header = $api.dom('.home-head');
                        var headerH = $api.fixStatusBar(header);
                        api.openFrame({
                            name: 'home_frm',
                            url: 'home_frm.html',
                            useWKWebView: true,
                            rect: {
                                marginTop: headerH,
                                marginBottom: api.pageParam.footerH,
                                w: 'auto'
                            },
                            pageParam: {
                                headerH: headerH,
                                footerH: api.pageParam.footerH,
                                selectedCityName: vm.selectedCityName
                            }
                        });
                    },
                    checkLocation() {
                        var vm = this
                        var resultList = api.hasPermission({
                            list: ['location']
                        });
                        var isPermit = resultList[0].granted;
                        if (isPermit) {
                            // 获取定位信息
                            vm.initLocation()
                        } else {
                            // 没有权限 请求权限
                            if ($api.getStorage('isLocationPerm') == 1) {

                            } else {
                                api.requestPermission({
                                    list: ['location'],
                                }, function (ret, err) {
                                    if (ret.list[0].granted) {
                                        // 获取定位信息
                                        vm.initLocation()
                                    } else {
                                        $api.setStorage('isLocationPerm', '1');
                                    }
                                });
                            }
                        }
                    },
                    initLocation() {
                        var vm = this;
                        var aMap = api.require('aMap');
                        aMap.open({
                            rect: { x: 0, y: 0, w: 0, h: 0 },
                            showUserLocation: true,
                            zoomLevel: 11,
                            center: { lon: 116.4021310000, lat: 39.9994480000 },
                            fixedOn: api.frameName,
                            fixed: true
                        }, function (ret, err) {
                            if (ret.status) {
                                // 获取经纬度
                                aMap.getLocation(function (ret1, err1) {
                                    if (ret1.status) {
                                        // 根据经纬度获取城市
                                        aMap.getNameFromCoords({ lon: ret1.lon, lat: ret1.lat }, (ret2, err2) => {
                                            if (ret2.status && ret2.address) {
                                                vm.selectedCityName = ret2.state

                                                if (ret2.citycode) {
                                                    index.area('getProvinceByCityCode')(ret2.citycode, function (ret) {
                                                        var location = {
                                                            name: ret2.state,
                                                            location: ret1.lon + "," + ret1.lat,
                                                        }
                                                        if (ret && ret.data) {
                                                            location.id = ret.data.id
                                                        }
                                                        $api.setStorage('location_home', location);

                                                        setTimeout(() => {
                                                            console.log("定位发111111111" + JSON.stringify(location))
                                                            api.sendEvent({
                                                                name: 'showLocation',
                                                                extra: { cityName: location.name }
                                                            })
                                                        }, 2000);

                                                    })
                                                }
                                            } else {
                                                vm.selectedCityName = '上海市'
                                                var location = {
                                                    name: '上海市',
                                                    location: '121.164311' + ',' + '31.046996',
                                                    id: 10796 // 上海市id为10796，从后端开发人员获取到
                                                }
                                                $api.setStorage('location_home', location);
                                                setTimeout(() => {
                                                    api.sendEvent({
                                                        name: 'showLocation',
                                                        extra: { cityName: location.name }
                                                    })
                                                }, 2000);
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    },

                    goPage(url) {
                        let vm = this
                        api.openWin({
                            name: url + '_win',
                            url: `../${url}/${url}_win.html`,
                            bounces: false,
                            pageParam: {
                                key: 'value',
                                kfUrl: vm.kfUrl,
                                titlePlus: '首页'
                            }
                        });
                    },
                    goRelative(url, relative) {
                        api.openWin({
                            name: url,
                            url: (relative || '') + url + ".html",
                            bounces: false,
                            pageParam: { title: 'home' }
                        })
                    },
                    getUnReadNum() {
                        var token = $api.getStorage('accessToken')
                        if (token) {
                            let vm = this
                            common('getUnReadNum')(function (ret) {
                                vm.unReadNum = ret.data
                            })
                        }
                    },
                },
            })
            // api.addEventListener({
            //     name: 'selectedCity'
            // }, function (ret) {
            //     vm.selectedCityName = ret.value.city.name;
            //     api.sendEvent({name: 'changeLocation'});
            // })
            // api.addEventListener({
            //     name: 'refreshUnReadMessageNum'
            // }, function(ret, err) {
            //     if (!ret.value.page || ret.value.page === 'home') {
            //         vm.getUnReadNum()
            //     }
            // });
        }
    </script>
</body>

</html>